home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / 2004-12 CHIP.iso / CHIP / Porady / Srodowisko PHP-MySQL / APACHE 2 ADD-ON / APACHE2_add-on.exe / {app} / Apache2 / bin / dbmmanage.pl < prev    next >
Perl Script  |  2004-06-29  |  9KB  |  311 lines

  1. #
  2. # Copyright 2001-2004 The Apache Software Foundation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. #     http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15.  
  16. #for more functionality see the HTTPD::UserAdmin module:
  17. # http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
  18. #
  19. # usage: dbmmanage <DBMfile> <command> <user> <password> <groups> <comment>
  20.  
  21. package dbmmanage;
  22. #                               -ldb    -lndbm    -lgdbm    -lsdbm
  23. BEGIN { @AnyDBM_File::ISA = qw(SDBM_File) }
  24. use strict;
  25. use Fcntl;
  26. use AnyDBM_File ();
  27.  
  28. sub usage {
  29.     my $cmds = join "|", sort keys %dbmc::;
  30.     die <<SYNTAX;
  31. Usage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
  32.  
  33.     where enc is  -d for crypt encryption (default except on Win32, Netware)
  34.                   -m for MD5 encryption (default on Win32, Netware)
  35.                   -s for SHA1 encryption
  36.                   -p for plaintext
  37.  
  38.     command is one of: $cmds
  39.  
  40.     pw of . for update command retains the old password
  41.     pw of - (or blank) for update command prompts for the password
  42.  
  43.     groups or comment of . (or blank) for update command retains old values
  44.     groups or comment of - for update command clears the existing value
  45.     groups or comment of - for add and adduser commands is the empty value
  46. SYNTAX
  47. }
  48.  
  49. sub need_sha1_crypt {
  50.     if (!eval ('require "Digest/SHA1.pm";')) {
  51.         print STDERR <<SHAERR;
  52. dbmmanage SHA1 passwords require the interface or the module Digest::SHA1
  53. available from CPAN:
  54.  
  55.     http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
  56.  
  57. Please install Digest::SHA1 and try again, or use a different crypt option:
  58.  
  59. SHAERR
  60.         usage();
  61.     }
  62. }
  63.  
  64. sub need_md5_crypt {
  65.     if (!eval ('require "Crypt/PasswdMD5.pm";')) {
  66.         print STDERR <<MD5ERR;
  67. dbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
  68.  
  69.     http://www.cpan.org/modules/by-module/Crypt/Crypt-PasswdMD5-1.1.tar.gz
  70.  
  71. Please install Crypt::PasswdMD5 and try again, or use a different crypt option:
  72.  
  73. MD5ERR
  74.         usage();
  75.     }
  76. }
  77.  
  78. # if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
  79. # four bytes of iteration count and four bytes of salt).  Otherwise, just use
  80. # the traditional two-byte salt.
  81. # see the man page on your system to decide if you have a newer crypt() lib.
  82. # I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
  83. # The new style crypt() allows up to 20 characters of the password to be
  84. # significant rather than only 8.
  85. #
  86. my $newstyle_salt_platforms = join '|', qw{bsdos}; #others?
  87. my $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
  88.  
  89. # Some platforms just can't crypt() for Apache
  90. #
  91. my $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
  92. my $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
  93.  
  94. my $crypt_method = "crypt";
  95.  
  96. if ($crypt_not_supported) {
  97.     $crypt_method = "md5";
  98. }
  99.  
  100. # Some platforms won't jump through our favorite hoops
  101. #
  102. my $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
  103. my $not_unix = $^O =~ /(?:$not_unix_platforms)/;
  104.  
  105. if ($crypt_not_supported) {
  106.     $crypt_method = "md5";
  107. }
  108.  
  109. if (@ARGV[0] eq "-d") {
  110.     shift @ARGV;
  111.     if ($crypt_not_supported) {
  112.         print STDERR 
  113.               "Warning: Apache/$^O does not support crypt()ed passwords!\n\n";
  114.     }
  115.     $crypt_method = "crypt";
  116. }
  117.  
  118. if (@ARGV[0] eq "-m") {
  119.     shift @ARGV;
  120.     $crypt_method = "md5";
  121. }
  122.  
  123. if (@ARGV[0] eq "-p") {
  124.     shift @ARGV;
  125.     if (!$crypt_not_supported) {
  126.         print STDERR 
  127.               "Warning: Apache/$^O does not support plaintext passwords!\n\n";
  128.     }
  129.     $crypt_method = "plain";
  130. }
  131.  
  132. if (@ARGV[0] eq "-s") {
  133.     shift @ARGV;
  134.     need_sha1_crypt();
  135.     $crypt_method = "sha1";
  136. }
  137.  
  138. if ($crypt_method eq "md5") {
  139.     need_md5_crypt();
  140. }
  141.  
  142. my($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
  143.  
  144. usage() unless $file and $command and defined &{$dbmc::{$command}};
  145.  
  146. # remove extension if any
  147. my $chop = join '|', qw{db.? pag dir};
  148. $file =~ s/\.($chop)$//;
  149.  
  150. my $is_update = $command eq "update";
  151. my %DB = ();
  152. my @range = ();
  153. my($mode, $flags) = $command =~ 
  154.     /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
  155.  
  156. tie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
  157. dbmc->$command();
  158. untie %DB;
  159.  
  160.  
  161. my $x;
  162. sub genseed {
  163.     my $psf;
  164.     if ($not_unix) {
  165.     srand (time ^ $$ or time ^ ($$ + ($$ << 15)));
  166.     }
  167.     else {
  168.         for (qw(-xlwwa -le)) { 
  169.         `ps $_ 2>/dev/null`;
  170.             $psf = $_, last unless $?;
  171.         }
  172.         srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
  173.     }
  174.     @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
  175.     $x = int scalar @range;
  176. }
  177.  
  178. sub randchar { 
  179.     join '', map $range[rand $x], 1..shift||1;
  180. }
  181.  
  182. sub saltpw_crypt {
  183.     genseed() unless @range; 
  184.     return $newstyle_salt ? 
  185.     join '', "_", randchar, "a..", randchar(4) :
  186.         randchar(2);
  187. }
  188.  
  189. sub cryptpw_crypt {
  190.     my ($pw, $salt) = @_;
  191.     $salt = saltpw_crypt unless $salt;
  192.     crypt $pw, $salt;
  193. }
  194.  
  195. sub saltpw_md5 {
  196.     genseed() unless @range; 
  197.     randchar(8);
  198. }
  199.  
  200. sub cryptpw_md5 {
  201.     my($pw, $salt) = @_;
  202.     $salt = saltpw_md5 unless $salt;
  203.     Crypt::PasswdMD5::apache_md5_crypt($pw, $salt);
  204. }
  205.  
  206. sub cryptpw_sha1 {
  207.     my($pw, $salt) = @_;
  208.     '{SHA}' . Digest::SHA1::sha1_base64($pw) . "=";
  209. }
  210.  
  211. sub cryptpw {
  212.     if ($crypt_method eq "md5") {
  213.         return cryptpw_md5(@_);
  214.     } elsif ($crypt_method eq "sha1") {
  215.         return cryptpw_sha1(@_);
  216.     } elsif ($crypt_method eq "crypt") {
  217.         return cryptpw_crypt(@_);
  218.     }
  219.     @_[0]; # otherwise return plaintext
  220. }
  221.  
  222. sub getpass {
  223.     my $prompt = shift || "Enter password:";
  224.  
  225.     unless($not_unix) { 
  226.     open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
  227.     system "stty -echo;";
  228.     }
  229.  
  230.     my($c,$pwd);
  231.     print STDERR $prompt;
  232.     while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
  233.     $pwd .= $c;
  234.     }
  235.  
  236.     system "stty echo" unless $not_unix;
  237.     print STDERR "\n";
  238.     die "Can't use empty password!\n" unless length $pwd;
  239.     return $pwd;
  240. }
  241.  
  242. sub dbmc::update {
  243.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  244.     $crypted_pwd = (split /:/, $DB{$key}, 3)[0] if $crypted_pwd eq '.';
  245.     $groups = (split /:/, $DB{$key}, 3)[1] if !$groups || $groups eq '.';
  246.     $comment = (split /:/, $DB{$key}, 3)[2] if !$comment || $comment eq '.';
  247.     if (!$crypted_pwd || $crypted_pwd eq '-') {
  248.         dbmc->adduser;
  249.     }
  250.     else {
  251.         dbmc->add;
  252.     }
  253. }
  254.  
  255. sub dbmc::add {
  256.     die "Can't use empty password!\n" unless $crypted_pwd;
  257.     unless($is_update) {
  258.     die "Sorry, user `$key' already exists!\n" if $DB{$key};
  259.     }
  260.     $groups = '' if $groups eq '-';
  261.     $comment = '' if $comment eq '-';
  262.     $groups .= ":" . $comment if $comment;
  263.     $crypted_pwd .= ":" . $groups if $groups;
  264.     $DB{$key} = $crypted_pwd;
  265.     my $action = $is_update ? "updated" : "added";
  266.     print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
  267. }
  268.  
  269. sub dbmc::adduser {
  270.     my $value = getpass "New password:";
  271.     die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
  272.     $crypted_pwd = cryptpw $value;
  273.     dbmc->add;
  274. }
  275.  
  276. sub dbmc::delete {
  277.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  278.     delete $DB{$key}, print "`$key' deleted\n";
  279. }
  280.  
  281. sub dbmc::view {
  282.     print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
  283. }
  284.  
  285. sub dbmc::check {
  286.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  287.     my $chkpass = (split /:/, $DB{$key}, 3)[0];
  288.     my $testpass = getpass();
  289.     if (substr($chkpass, 0, 6) eq '$apr1$') {
  290.         need_md5_crypt;
  291.         $crypt_method = "md5";
  292.     } elsif (substr($chkpass, 0, 5) eq '{SHA}') {
  293.         need_sha1_crypt;
  294.         $crypt_method = "sha1";
  295.     } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
  296.         $crypt_method = "crypt";
  297.     } else {
  298.         $crypt_method = "plain";
  299.     }
  300.     print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass 
  301.                            ? " password ok\n" : " password mismatch\n");
  302. }
  303.  
  304. sub dbmc::import {
  305.     while(defined($_ = <STDIN>) and chomp) {
  306.     ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;
  307.     dbmc->add;
  308.     }
  309. }
  310.  
  311.